<template>
  <div class="dialog">
    <el-dialog :title="'员工' + titleText" :visible.sync="showDialog" width="40%" :close-on-click-modal="false"
      @close="cancel">
      <el-form ref="ruleForm"  :model="formData" :rules="rules"  label-width="120px" class="demo-ruleForm">
        <el-form-item label="员工名称"   prop="staffName">
          <el-input v-model="formData.staffName" clearable placeholder="请输入员工名称" />
        </el-form-item>
        <el-form-item label="性别"   prop="sex">
          <el-select class="w100p" filterable clearable v-model="formData.sex" placeholder="请选择">
            <el-option label="男" :value="true" />
            <el-option label="女" :value="false" />
          </el-select>
        </el-form-item>
        <el-form-item label="联系电话"   prop="tel">
          <el-input v-model="formData.tel" clearable placeholder="请输入" />
        </el-form-item>
        <el-form-item label="所属部门"   prop="departmentId">
          <el-select class="w100p" filterable clearable v-model="formData.departmentId"  placeholder="请选择" @change="queryPost">
            <el-option v-for="(item, index) of departmentList" :key="index + 'department'" :label="item.departmentName"
              :value="item.departmentId" />
          </el-select>
        </el-form-item>
        <el-form-item label="所属岗位"   prop="postId">
          <el-select class="w100p" filterable clearable v-model="formData.postId" placeholder="请先选择部门">
            <el-option v-for="(item, index) of postList" :key="index + 'post'" :label="item.postName"
              :value="item.postId" />
          </el-select>
        </el-form-item>
        <el-form-item label="是否在职"   prop="activeFlag">
          <el-select class="w100p" filterable clearable v-model="formData.activeFlag"  placeholder="请选择">
            <el-option v-for="(item, index) of flagList" :key="index + 'flag'" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="身份证号"   prop="idCard">
          <el-input v-model="formData.idCard" clearable placeholder="请输入" />
        </el-form-item>
        <el-form-item label="微信"   prop="weChat">
          <el-input v-model="formData.weChat" clearable placeholder="请输入" />
        </el-form-item>
        <el-form-item label="邮箱"   prop="email">
          <el-input v-model="formData.email" clearable placeholder="请输入" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="cancelbtn" @click="cancel()">取 消</el-button>
        <el-button type="primary" class="savebtn" @click="save('ruleForm')">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import StaffApi from "@/api/basic-info-manage/staff";
import DepartmentApi from "@/api/basic-info-manage/department";
import PostApi from "@/api/basic-info-manage/post";
export default {
  name: '',
  components: {},
  props: {
    // 弹框名称
    titleText: {
      type: String,
      default: ""
    },
    nowChanceStau: {
      type: String,
      default: ""
    },
    show: {
      type: Boolean,
      default: false
    },
    editObject: {
      type: [Object],
      default: () => {
        return {}
      }
    },
  },
  data () {
    let validateEmail = (rule, email, callback) => {
      const re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
      if (email) {
        if (re.test(String(email).toLowerCase())) {
          callback();
        } else {
          return callback(new Error("邮箱地址不合法"));
        }
      } else {
        callback()
      }
    };
    let validatePhoneNumber = (rule, phoneNumber, callback) => {
      const re = /^1[3456789]\d{9}$/;
      if (phoneNumber) {
        if (re.test(String(phoneNumber))) {
          callback();
        } else {
          return callback(new Error("手机号不合法"));
        }
      } else {
        callback()
      }
    };
    let validateIDNumber = (rule, idNumber, callback) => {
      const re = /^[1-9]\d{5}(19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
      if (idNumber) {
        if (re.test(String(idNumber))) {
          callback();
        } else {
          return callback(new Error("身份证号不合法"));
        }
      } else {
        callback()
      }
    };
    return {
      formData: {},
      flagList: [
        { label: "是", value: true },
        { label: "否", value: false },
      ],
      departmentList: [],
      postList: [],
      rules: {
        staffName: [
          { required: true, message: "请输入员工名称", trigger: ["change", "blur"] }
        ],
        tel: [
          { required: true, message: "请输入联系电话", trigger: ["change", "blur"] },
          { validator: validatePhoneNumber, trigger: ["blur"] }
        ],
        email: [
          { validator: validateEmail, trigger: ["blur"] }
        ],
        idCard: [
          { validator: validateIDNumber, trigger: ["blur"] }
        ],
        departmentId: [
          { required: true, message: "请选择归属部门", trigger: ["change", "blur"] }
        ],
        postId: [
          { required: true, message: "请选择所属岗位", trigger: ["change", "blur"] }
        ],
        sex: [
          { required: true, message: "请选择性别", trigger: ["change", "blur"] }
        ],
        activeFlag: [
          { required: true, message: "请选择是否在职", trigger: ["change", "blur"] }
        ],
      },
      showDialog: this.show
    };
  },

  watch: {
    show (v) {
      this.showDialog = v;
      this.querydepartment();
    },
    nowChanceStau (v) {
      if (v === 'add') {
        this.formData = {
        }
        this.$nextTick(() => {
          this.$refs['ruleForm'].clearValidate()
        })
      }
      if (v === 'edit') {
        this.queryPost(this.editObject.departmentId)
        this.formData = this.editObject
        this.formData.postId = this.editObject.postId
      }
    }
  },
  created () {
  },
  mounted () {
    this.querydepartment()
  },
  methods: {
    querydepartment () {
      DepartmentApi.queryByCondition().then(res => {
        this.departmentList = res.data
      })
    },
    queryPost (e) {
      PostApi.queryByCondition({ departmentId: e }).then(res => {
        this.postList = res.data
      })
    },
    save (formData) {
      console.log(this.$refs[formData].validate);
      this.$refs[formData].validate(valid => {
        if (valid) {
          if (this.nowChanceStau === "edit") {
            StaffApi.edit(this.formData).then(res => {
              if (res.success) {
                this.$message.success("修改成功！");
                this.close()
              } else {
                this.$message.error("系统错误,请稍后再试");
              }
            });
          } else if (this.nowChanceStau === "add") {
            StaffApi.add(this.formData).then(res => {
              if (res.success) {
                this.$message.success("添加成功");
                this.close()
              } else {
                this.$message.error("系统错误,请稍后再试");
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    close () {
      this.$emit("closeDialog");
    },
    cancel () {
      this.$emit("cancelDialog");
    }
  }
};
</script>

<style lang="scss" scoped></style>
